package com.cy.ppj.comm.third.xy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by rayychoi on 2018/1/26.
 */
public class HttpsRestClient {

    private RestTemplate restTemplate;


    public HttpsRestClient(){
        try {
            restTemplate = new RestTemplate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    }

    public HttpsRestClient(String certFilePath, String keyFilePath){
        ClientHttpRequestFactoryCreator creator = new ClientHttpRequestFactoryCreator(certFilePath, keyFilePath);
        try {
            restTemplate = new RestTemplate(creator.httpClientRequestFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
//        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    }

    public String post(String requestUrl, HttpHeaders headers, String json){
        HttpEntity<String> request = new HttpEntity<String>(json, headers);//将参数和header组成一个请求
        ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, request, String.class);
        return response.getBody();
    }

    public String post(String requestUrl, String json){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> request = new HttpEntity<String>(json, headers);//将参数和header组成一个请求
        ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, request, String.class);
        return response.getBody();
    }

//    public static void main(String[] args) throws JsonProcessingException, FileNotFoundException {
//
//        HttpsRestClient client = new HttpsRestClient("/Users/Ocean/gitee/ppj/src/main/resources/file/xy/xiaoying_client.crt", "/Users/Ocean/gitee/ppj/src/main/resources/file/xy/xiaoying_client_key.der");
//        Map<String, String> params = new HashMap<>();//参数放入一个map中，restTemplate不能用hashMap
//        params.put("user_name","史超");
//        params.put("idcard_no","360103199001221727");
//        params.put("partner_id","426");
//        params.put("mobile", "18856565656");
//        params.put("appid", "10002");
//        String requestUrl = "https://rc.xiaoying.com/api/out/apply/searchuser";
//        String json = client.post(requestUrl, new ObjectMapper().writeValueAsString(params));
//        System.out.println(json);
//    }

}
